package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import DAO.BaseDAO;
import DTO.Pl;
import pageUtil.PageB;
import service.PageService;
import serviceImpl.PlDAO;

@WebServlet(name = "pl",urlPatterns = "/pl")
public class PlServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public PlServlet() {
		
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	doPost(request, response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        service(request, response);
    }
    
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	System.out.println("进入了评论处理Servlet中");
        String flag = null;
        flag = request.getParameter("flag");
        switch (flag) {
            case "findall":
                try {
                    find_all(request,response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "delete":
                try {
                    delete(request,response);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }
    
    protected static void find_all(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	String sql = "select a.id,b.name as spname,a.plxx,a.plsj,a.bz,c.username as username,a.status from pl a,merchaned b,admin c where a.spid = b.id and a.plrid = c.id and a.status=1 limit ?,?";
        String sql2 = "select count(id) from pl";
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String currentPage = request.getParameter("cp");
        currentPage = currentPage == null?"1":currentPage;
        //创建一个分页工具对象
        PageB<Pl> pageBean = new PageB<Pl>();
        pageBean.setCurrentPage(Integer.parseInt(currentPage));
        pageBean.setPageSize(5);
        PageService<Pl> pageservice = new PageService<Pl>(new PlDAO());
        PageB<Pl> bean = pageservice.paging(pageBean,sql,sql2);
        //将java对象转为JSON字符串
        String page = JSON.toJSONString(bean);
        PrintWriter out = response.getWriter();
        out.print(page);
        out.flush();
    }
    
    protected static void delete(HttpServletRequest request , HttpServletResponse response) throws Exception {
    	try {
   		 	response.encodeRedirectURL("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html; charset=UTF-8");
            request.setCharacterEncoding("utf-8");
            String ID = request.getParameter("id");
            Pl pl = new Pl();
            BaseDAO<Pl> ed = new PlDAO();
            String sql = "update pl set status=0 where id=?";
            pl.setId(Integer.parseInt(ID));
            if(ed.update(pl, sql)){
                request.setAttribute("msg","屏蔽成功");
                request.getRequestDispatcher("pl_list.jsp").forward(request, response);
            }else{
                request.setAttribute("msg","屏蔽失败");
                request.getRequestDispatcher("pl_list.jsp").forward(request, response);

            }
            
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
